<?

require "secret/config.php";
mysql_connect($dbhost,$dbusername,$dbpassword) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$query = "select distinct userid, spielername, status, ally from ogame_galaxy b1 where not exists(select * from ogame_galaxy b2 where b2.userid=b1.userid and b2.spielername=b1.spielername and b2.zeit>b1.zeit)";
$res = mysql_query($query);
if(!$res) {
	echo mysql_error($res);
	die("die");
}
while($line=mysql_fetch_array($res)) {
	$userid = $line['userid'];
	$playername = $line['spielername'];
	$status = $line['status'];
	$ally = $line['ally'];
	echo "$userid, $playername, $status, $ally <br/>";
	$query = "INSERT IGNORE into ogame_playerstats(userid, playername) values($userid, '$playername')";
	mysql_query($query) or die(mysql_error());
	$query = "UPDATE ogame_playerstats SET status='$status', ally='$ally' where userid=$userid and playername='$playername'";
	mysql_query($query) or die(mysql_error());
}

?>